package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppGoodsInfoBo;
import com.ruoyi.app.domain.vo.AppGoodsInfoVo;
import com.ruoyi.app.domain.AppGoodsInfo;
import com.ruoyi.app.mapper.AppGoodsInfoMapper;
import com.ruoyi.app.service.IAppGoodsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP商城订单关联商品Service业务层处理
 *
 * @author heike
 * @date 2023-03-02
 */
@RequiredArgsConstructor
@Service
public class AppGoodsInfoServiceImpl implements IAppGoodsInfoService {

    private final AppGoodsInfoMapper baseMapper;

    /**
     * 查询APP商城订单关联商品
     */
    @Override
    public AppGoodsInfoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public List<AppGoodsInfoVo> queryOrderNo(String orderNo) {
        LambdaQueryWrapper<AppGoodsInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppGoodsInfo::getOrderNo, orderNo);
        return baseMapper.selectVoList(wrapper);
    }

    /**
     * 查询APP商城订单关联商品列表
     */
    @Override
    public TableDataInfo<AppGoodsInfoVo> queryPageList(AppGoodsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppGoodsInfo> lqw = buildQueryWrapper(bo);
        Page<AppGoodsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP商城订单关联商品列表
     */
    @Override
    public List<AppGoodsInfoVo> queryList(AppGoodsInfoBo bo) {
        LambdaQueryWrapper<AppGoodsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppGoodsInfo> buildQueryWrapper(AppGoodsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppGoodsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), AppGoodsInfo::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getGoodsId() != null, AppGoodsInfo::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getNumber() != null, AppGoodsInfo::getNumber, bo.getNumber());
        lqw.eq(bo.getGoodsPrice() != null, AppGoodsInfo::getGoodsPrice, bo.getGoodsPrice());
        return lqw;
    }

    /**
     * 新增APP商城订单关联商品
     */
    @Override
    public Boolean insertByBo(AppGoodsInfoBo bo) {
        AppGoodsInfo add = BeanUtil.toBean(bo, AppGoodsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP商城订单关联商品
     */
    @Override
    public Boolean updateByBo(AppGoodsInfoBo bo) {
        AppGoodsInfo update = BeanUtil.toBean(bo, AppGoodsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public Boolean inserts(List<AppGoodsInfo> appGoodsInfoBos) {
        return baseMapper.insertBatch(appGoodsInfoBos);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppGoodsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP商城订单关联商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
